package com.demon.framework.boot.failover;

import com.demon.framework.common.model.constants.DateConstants;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.io.Serializable;
import java.util.Date;

/**
 * 业务重试 bean
 *
 * @author Demon-HY
 * @date 2020/7/24 16:42
 */
@Data
@NoArgsConstructor
public class TaskFailoverBean implements Serializable {

    private static final long serialVersionUID = 8980308435220597507L;

    private final static String TABLE_NAME = "task_failover";
    /**
     * 重试的job分组
     */
    public final static String JOB_GROUP_FOR_FAILOVER = "failover";

    protected final static BeanPropertyRowMapper<TaskFailoverBean> BEAN_BEAN_PROPERTY_ROW_MAPPER
            = new BeanPropertyRowMapper<>(TaskFailoverBean.class);

    /**
     * 按ID查询的sql
     */
    protected static String getSqlForFindById(Long id) {
        return "SELECT id,type,name,url,retry_count as 'retryCount',data,serialize_data as 'serializeData',reason,create_time as 'createTime',update_time as 'updateTime',next_time as 'nextTime' FROM " + TABLE_NAME + " WHERE id=" + id;
    }

    /**
     * 按type和name查询的sql
     */
    protected static String getSqlForFindByTypeAndName(String type, String name) {
        return "SELECT id,type,name,url,retry_count as 'retryCount',data,serialize_data as 'serializeData',reason,create_time as 'createTime',update_time as 'updateTime',next_time as 'nextTime' FROM " + TABLE_NAME + " WHERE type='" + type + "' AND name='" + name + "'";
    }

    /**
     * 添加重试业务的sql, data字段长度 4096,reason字段长度 1024
     */
    protected String getSqlForAdd() {
        return "insert into " + TABLE_NAME + " (type,name,url,retry_count,data,serialize_data,reason,create_time,update_time,next_time) VALUES ("
                + "'" + type + "','" + name + "','" + url + "',0,'" + StringUtils.left(data, 4096) + "','" + serializeData + "','" + StringUtils.right(reason, 1024) + "',now(),now(),'" + DateFormatUtils.format(nextTime, DateConstants.yyyyMMddHHmmssSSS_D) + "')";
    }

    /**
     * 重试失败业务的sql
     */
    protected String getSqlForRetry() {
        return "UPDATE " + TABLE_NAME + " SET retry_count=retry_count+1,update_time=now(),reason='" + reason + "', next_time='" + DateFormatUtils.format(nextTime, DateConstants.yyyyMMddHHmmssSSS_D) + "' WHERE id=" + id;
    }

    /**
     * 重试清除执行的sql
     */
    protected static String getSqlForClear(long id) {
        return "DELETE FROM " + TABLE_NAME + " WHERE id=" + id;
    }

    private Long id;

    // 重试的分类
    private String type;

    // 重试的任务名称
    private String name = StringUtils.EMPTY;

    // 请求的rest接口地址
    private String url;

    // 当前重试的次数
    private Integer retryCount;

    // 数据包内容 json 存储
    private String data;
    // http请求的完整数据包，用于接口重放
    private String serializeData;

    // 失败原因,以|号分隔
    private String reason;

    // 创建时间
    private Date createTime;

    // 更新时间
    private Date updateTime;

    // 下次重试时间
    private Date nextTime;

    @Override
    public String toString() {
        return "TaskFailoverBean{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", url='" + url + '\'' +
                ", retryCount=" + retryCount +
                ", data='" + data + '\'' +
                ", reason='" + reason + '\'' +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", nextTime=" + nextTime +
                '}';
    }
}